package com.kizitonwose.calendarview.utils;

import com.kizitonwose.calendarview.NumberRange;

import java.time.DayOfWeek;
import java.time.temporal.WeekFields;
import java.util.*;

public class DayExtensions {

    public static DayOfWeek[] daysOfWeekFromLocale() {
        DayOfWeek firstDayOfWeek = WeekFields.of(Locale.getDefault()).getFirstDayOfWeek();
        DayOfWeek[] daysOfWeek = DayOfWeek.values();
        // Order `daysOfWeek` array so that firstDayOfWeek is at index 0.
        // Only necessary if firstDayOfWeek != DayOfWeek.MONDAY which has ordinal 0.
        if (firstDayOfWeek != DayOfWeek.MONDAY) {
            DayOfWeek[] rhs = ArrayExtension.getInstance(daysOfWeek).sliceArray(new NumberRange(firstDayOfWeek.ordinal(), daysOfWeek.length));
            DayOfWeek[] lhs = ArrayExtension.getInstance(daysOfWeek).sliceArray(new NumberRange(0, firstDayOfWeek.ordinal()));
            List<DayOfWeek> list = new ArrayList<>(Arrays.asList(rhs));
            Collections.addAll(list, lhs);
            daysOfWeek = list.toArray(daysOfWeek);
        }
        return daysOfWeek;
    }
}
